/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import core.Group;
import db.dao.GroupDAO;
import db.dao.StudentDAO;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Mikael
 */
public class GroupDataTableBean {
    private ArrayList<Group> groups;
    private ArrayList<Group> currentGroups;
    private int headerCnt;
    //private String currHeader;

    private String searchName;
    private String searchCreator;

    public static int COLUMN_COUNT = 3;
    private String[] headers = new String[COLUMN_COUNT];

    public String getCurrHeader(){
        String header = headers[headerCnt];
        headerCnt++;
        headerCnt %= COLUMN_COUNT;
        return header;
    }

    @PostConstruct
    public void init(){
        headers[0] = "Name";
        headers[1] = "Code";
        headers[2] = "Description";

        groups = new ArrayList();
        currentGroups = new ArrayList();

        searchName = "";
        searchCreator = "";
    }

    public List<Group> getGroups(){
        groups.clear();
        groups = (ArrayList)GroupDAO.search(
                new Group(0,searchName,"",0,searchCreator));
        //(ArrayList)CourseDAO.readAll();
        return groups;
    }

    public void setGroups(ArrayList<Group> groups){
        this.groups = groups;
    }

    public List<Group> getCurrentGroups() {
        //Get active login id by getting login session bean
        FacesContext facesCont = FacesContext.getCurrentInstance();
        HttpSession https = (HttpSession)facesCont.getExternalContext().getSession(false);
        LoginBean lb = (LoginBean)https.getAttribute(LoginBean.MANGED_BEAN_NAME);

        currentGroups.clear();
        currentGroups = (ArrayList)StudentDAO.getGroups(lb.getId());
        return currentGroups;
    }

    public void setCurrentGroups(ArrayList<Group> currentGroups) {
        this.currentGroups = currentGroups;
    }

    public String getSearchCreator() {
        return searchCreator;
    }

    public void setSearchCreator(String searchCreator) {
        this.searchCreator = searchCreator;
    }

    public String getSearchName() {
        return searchName;
    }

    public void setSearchName(String searchName) {
        this.searchName = searchName;
    }
}
